//
//  Controller24.swift
//  Swift30Days
//
//  Created by licheng ke on 2017/3/23.
//  Copyright © 2017年 licheng ke. All rights reserved.
//

import UIKit

//custom collection layout
class Controller24: UIViewController,UICollectionViewDelegate,UICollectionViewDataSource {

    let datasCount = 13
    var collectionView : UICollectionView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configUI()
    }
    
    func configUI(){
        collectionView = UICollectionView(frame: .zero, collectionViewLayout: CustomLayout())
        view.addSubview(collectionView)
        collectionView.backgroundColor = UIColor(white: 1, alpha: 1)
        collectionView.snp.makeConstraints { (maker) in
            maker.top.equalTo(topLayoutGuide.snp.top)
            maker.bottom.equalTo(bottomLayoutGuide.snp.top)
            maker.left.equalTo(0)
            maker.right.equalTo(view.snp.right)
        }
        collectionView.dataSource = self
        collectionView.delegate = self
//        collectionView.register(UINib(nibName:"ColorCollectionViewCell", bundle: nil), forCellWithReuseIdentifier: "aaa")
        collectionView.register(ColorfulCollectionCell.classForCoder(), forCellWithReuseIdentifier: "aaa")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    //MARK: - collectionview
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return datasCount
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "aaa", for: indexPath)
        cell.alpha = 0
        
        let time = DispatchTime.now() + .milliseconds(800)
        DispatchQueue.main.asyncAfter(deadline: time) { 
            UIView.animate(withDuration: 0.3) {
                cell.alpha = 1
            }
        }
        return cell
    }
    

}
